package com.cxf.cxfzuul;

import com.cxf.cxfzuul.annotation.UserLoginToken;
import com.cxf.cxfzuul.repository.ResDto;
import com.cxf.cxfzuul.repository.User;
import com.cxf.cxfzuul.service.UserService;
import com.cxf.cxfzuul.utils.TokenUtils;
import com.fasterxml.jackson.databind.util.JSONPObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * description: UserController <br>
 * date: 2021/11/3 21:27 <br>
 * author: cxf <br>
 * version: 1.0 <br>
 */

@RestController
@RequestMapping("api")
public class UserController {

    @Autowired
    UserService userService;
    @Autowired
    TokenUtils tokenUtils;

    /**
     * @Title: login
     * @Description: http://169.254.249.67:9945/api/login
     * {
     *   "id": "817309",
     *   "username": "cxf",
     *   "password": "cxf123456"
     * }
     * @Author: cxf
     * @DateTime: 2021/11/3 22:09
     * @param user
     * @return java.lang.Object
     * @throws
     */

    @PostMapping("/login")
    public Object login(@RequestBody User user){

        ResDto resDto = new ResDto();

        User userForBase=userService.findByUsername(user);
        if(userForBase==null){
            resDto.setResMsg("登录失败,用户不存在");

        }else {
            if (!userForBase.getPassword().equals(user.getPassword())){
                resDto.setResMsg("登录失败,密码错误");

            }else {
                String token = tokenUtils.getToken(userForBase);
                resDto.setToken(token);
                resDto.setData(userForBase);
            }
        }

        return resDto;
    }


    /**
     * @Title: getMessage
     * @Description: http://169.254.249.67:9945/api/getMessage
     * @Author: cxf
     * @DateTime: 2021/11/3 22:09
     * @param
     * @return java.lang.String
     * @throws
     */
    @UserLoginToken
    @GetMapping("/getMessage")
    public String getMessage(){
        return "你已通过验证";
    }

}
